import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, QVBoxLayout, QHBoxLayout, QPushButton, \
    QTableWidget, QTableWidgetItem


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        # 标题和尺寸
        self.setWindowTitle("模组更新")
        self.resize(828, 500)
        # 设置窗体居中
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        # 设置布局
        layout = QVBoxLayout()
        layout.addLayout(self.init_header())
        layout.addLayout(self.init_table())
        layout.addLayout(self.init_footer())
        # 添加弹簧填充底部区域
        layout.addStretch()
        # 渲染布局
        self.setLayout(layout)

    # 顶部布局
    def init_header(self):
        # 顶部布局
        header_layout = QHBoxLayout()
        # 顶部控件
        btn_start = QPushButton("开始")
        header_layout.addWidget(btn_start)

        btn_stop = QPushButton("停止")
        header_layout.addWidget(btn_stop)

        header_layout.addStretch()
        return header_layout

    # 表格布局
    def init_table(self):
        table_layout = QHBoxLayout()
        table_header = [
            {"title": "同步", "width": 80},
            {"title": "路径", "width": 350},
            {"title": "哈希值", "width": 350},
        ]
        table_widget = QTableWidget(0, len(table_header))
        for idx, info in enumerate(table_header):
            table_item = QTableWidgetItem(info["title"])
            table_widget.setHorizontalHeaderItem(idx, table_item)
            table_widget.setColumnWidth(idx, info["width"])
        # 初始化数据
        data = [["√", "/xxx.rar", "123456789"], ["√", "/xxx.rar", "123456789"]]
        current_row_count = table_widget.rowCount()
        for row_list in data:
            table_widget.insertRow(current_row_count)
            for i, ele in enumerate(row_list):
                cell = QTableWidgetItem(str(ele))
                cell.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
                table_widget.setItem(current_row_count, i, cell)
            current_row_count += 1
        # 完成布局
        table_layout.addWidget(table_widget)
        return table_layout

    # 底部布局
    def init_footer(self):
        footer_layout = QHBoxLayout()
        return footer_layout


app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
